package com.huang.wangpan.strategy.process;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huang.wangpan.dto.FileIm;
import com.huang.wangpan.dao.UserFileDao;
import com.huang.wangpan.dao.UserDao;
import com.huang.wangpan.vo.UserFile;
import com.huang.wangpan.utils.CommonUtils;
import com.huang.wangpan.utils.PathAnalysis;
import com.huang.wangpan.utils.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: liminghui
 * @Date: 2020/11/7 12:37
 * @Version:
 * @Description:
 */
@Component
public class GetSuperiorProcess implements CommonProcess {
    @Autowired
    UserFileDao userFileDao;
    @Autowired
    UserDao userDao;

    private static final Logger logger = LoggerFactory.getLogger(GetSuperiorProcess.class);

    @Override
    public void process(String method, HttpServletRequest request, HttpServletResponse response) {
        // 获得上一级的文件
        logger.info("GetSuperiorProcess-->process 获取上一级开始......");
        ResultData rd = new ResultData();
        String currentpath = request.getParameter("currentpath");
        String Catalogue = request.getParameter("Catalogue");
        String newpath = "/";
        String[] paths = currentpath.split("/");
        for (int i = 0; i < paths.length - 1; i++) {
            if (paths[i] != null && paths[i].trim().length() != 0)
                newpath += paths[i] + "/";
        }
        int page = Integer.parseInt(Catalogue);
        if (page > 0) {
            PathAnalysis pa = new PathAnalysis();
            String token = CommonUtils.loadToken(request);
            String name = userDao.selectNameByToken(token);
            List<UserFile> ufs = userFileDao.searchByName(name);
            List<FileIm> fms = pa.SuperiorCatalogue(ufs, newpath, page);
            JSONObject message = new JSONObject();
            JSONArray data = new JSONArray();
            for (FileIm temp : fms) {
                JSONObject tempj = new JSONObject();
                tempj.put("filesize", temp.getFilesize());
                tempj.put("filename", temp.getFilename());
                tempj.put("date", temp.getMtime());
                tempj.put("iconsign", temp.getIconsign());
                data.add(tempj);
            }

            logger.info("GetSuperiorProcess-->process newpath= {}", newpath);
            JSONObject msg = new JSONObject();
            msg.put("Catalogue", page - 1);
            msg.put("currentpath", newpath);
            // rd.setData(data);
            message.put("code", 0);
            message.put("msg", msg);
            message.put("data", data);
            rd.setData(message);
            rd.writeToResponse(response);
            logger.info("GetSuperiorProcess-->process 上级目录更新完毕！");
        }
    }
}
